import { format, isToday, isYesterday, isThisYear } from 'date-fns';

const DATE_TIME_FORMAT = 'yyyy-MM-dd HH:mm:ss';
const DATE_FORMAT = 'yyyy-MM-dd';
const TIME_FORMAT = 'HH:mm';
const DATE_TIME_FORMAT_UTC = 'yyyy-MM-dd\'T\'HH:mm:ss.SSS';

/** 计算时间：比较当前时间，计算出返回时间的值 */
export function computeTime(date: Date | number): string {
  // 今天
  if (isToday(date)) {
    return formatToTime(date);
  }
  // 昨天
  if (isYesterday(date)) {
    return `昨天 ${formatToTime(date)}`;
  }
  // 今年
  if (isThisYear(date)) {
    return formatToDateTime(date, 'MM-dd HH:mm');
  }
  return formatToDate(date);
}

export function formatToDateTimeUTC(date: Date | number, formatStr = DATE_TIME_FORMAT_UTC): string {
  return format(date, formatStr);
}

export function formatToDateTime(date: Date | number, formatStr = DATE_TIME_FORMAT): string {
  return format(date, formatStr);
}

export function formatToDate(date: Date | number, formatStr = DATE_FORMAT): string {
  return format(date, formatStr);
}

export function formatToTime(date: Date | number, formatStr = TIME_FORMAT): string {
  return format(date, formatStr);
}
